package tech.aistar.day03.cookies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet(urlPatterns = "/cookie/get")
public class CookieGetController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //检索cookie
        Cookie[] cookies = req.getCookies();
        if(null!=cookies && cookies.length>0){
            for (Cookie c : cookies) {
                //String getName();//获取cookie的名称
                if("username".equals(c.getName())){
                    //接着获取cookie的value
                    String value = c.getValue();
                    //由于我们的value是中文 - 经过了二级编码然后进行存储的
                    //需要啊进行二级解码
                    value = URLDecoder.decode(value,"utf-8");
                    value = URLDecoder.decode(value,"utf-8");
                    System.out.println(c.getName()+":"+value);
                }
            }
        }
    }
}
